package com.cmccgz.udacs_landlord_webapp.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmccgz.udacs_landlord_webapp.R;
import com.jcodecraeer.xrecyclerview.ArrowRefreshHeader;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.SimpleViewSwithcer;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jcodecraeer.xrecyclerview.progressindicator.AVLoadingIndicatorView;

import java.lang.reflect.Field;

/**
 * xrecyclerview反射改源码工具类
 * @author INX
 */
public class XRecyclerViewReflectUtils {
    /**
     * 替换xrecylerview的下拉头
     * @param rv
     */
    public static void setLogoHeader(XRecyclerView rv){
        try {
            Context mContext=rv.getContext();
            ArrowRefreshHeader mRefreshHeader = (ArrowRefreshHeader) getField(XRecyclerView.class,rv, "mRefreshHeader");
            LinearLayout mContainer = (LinearLayout) getField(mRefreshHeader, "mContainer");
            //移除旧的header
            mRefreshHeader.removeView(mContainer);
            View header = LayoutInflater.from(mContext).inflate(R.layout.yd_header, null);
            //添加新的header
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(0, 0, 0, 0);
            mRefreshHeader.setLayoutParams(lp);
            mRefreshHeader.setPadding(0, 0, 0, 0);
            mRefreshHeader.addView(header, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0));
            setField(mRefreshHeader,"mContainer",header);
            mRefreshHeader.setGravity(Gravity.BOTTOM);
            //替换新header的内容
            setField(mRefreshHeader,"mArrowImageView",header.findViewById(R.id.listview_header_arrow));
            setField(mRefreshHeader,"mStatusTextView",header.findViewById(R.id.refresh_status_textview));
            setField(mRefreshHeader,"mProgressBar",header.findViewById(R.id.listview_header_progressbar));
            setField(mRefreshHeader,"mHeaderTimeView",header.findViewById(R.id.last_refresh_time));
            ImageView mArrowImageView = (ImageView) getField(mRefreshHeader, "mArrowImageView");
            TextView mStatusTextView = (TextView) getField(mRefreshHeader, "mStatusTextView");
            SimpleViewSwithcer mProgressBar = (SimpleViewSwithcer) getField(mRefreshHeader, "mProgressBar");
            TextView mHeaderTimeView = (TextView) getField(mRefreshHeader, "mHeaderTimeView");

            AVLoadingIndicatorView progressView = new  AVLoadingIndicatorView(mContext);
            progressView.setIndicatorColor(0xffB5B5B5);
            progressView.setIndicatorId(ProgressStyle.BallSpinFadeLoader);
            mProgressBar.setView(progressView);

            /*
		measure(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		mMeasuredHeight = getMeasuredHeight();*/
            mRefreshHeader.measure(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            setField(mRefreshHeader,"mMeasuredHeight",mRefreshHeader.getMeasuredHeight());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置反射属性
     * @param link
     * @param fieldName
     * @param fieldValue
     */
    private static void setField(Object link, String fieldName, Object fieldValue){
      setField(link.getClass(),link,fieldName,fieldValue);
    }

    /**
     * 获取反射属性
     * @param link
     * @param fieldName
     * @return
     */
    private static Object getField(Object link, String fieldName){
        return getField(link.getClass(),link,fieldName);
    }
    /**
     * 获取反射属性
     * @param link
     * @param fieldName
     * @return
     */
    private static Object getField(Class<?> clazz,Object link, String fieldName){
        try {
            Field declaredField = clazz.getDeclaredField(fieldName);
            declaredField.setAccessible(true);
            return declaredField.get(link);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 设置反射属性
     * @param link
     * @param fieldName
     * @param fieldValue
     */
    private static void setField(Class<?> clazz,Object link, String fieldName, Object fieldValue){
        try {
            Field declaredField = clazz.getDeclaredField(fieldName);
            declaredField.setAccessible(true);
            declaredField.set(link,fieldValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
